/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.arena;

import com.cmp.screens.GameScreen;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import java.util.Stack;

/**
 * Handles announcer msgs and sounds depending on game situations n stuff.
 * @author Krummer
 */
public class Announcer extends AbstractAppState {
    
    private static final float TEXT_TIME = 5.f;
    private float textTime;
    private Element announceText;
    private Stack msgStack;
    
    public void addAnnouncement(String text) {
        this.msgStack.add(text);
    }
    
     /* app state overrides */ 
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
       super.initialize(stateManager, app);
       
       this.msgStack = new Stack();
       this.textTime = TEXT_TIME;
       
       //load element from screen.
       this.announceText = app.getStateManager()
                              .getState(GameScreen.class).getMsgLabel();
       
       msgStack.push("START GAME");
    }
    
    @Override
    public void update(float tpf) {
        
       if(textTime > 0.f) {
            
          this.textTime -= tpf;
          if(textTime <= 0.f) {
              this.textTime = 0.f;
          }
       }
       
       if(!this.msgStack.isEmpty() && (this.textTime == 0.f)  && (this.announceText != null)) {
           this.announceText.getRenderer(TextRenderer.class).setText(msgStack.pop().toString());
           this.textTime = TEXT_TIME;
           
       } else if(this.msgStack.isEmpty() && (this.textTime == 0.f) && (this.announceText != null)) {
           this.announceText.getRenderer(TextRenderer.class).setText("");
       }
    }   
}
